package com.xieyingwangluo.yxpart.logic;

import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.bean.SkillDTO;
import com.xieyingwangluo.yxpart.entity.SkillModel;
import com.xieyingwangluo.yxpart.service.SkillService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: wangchaonan
 * @Date: 2018/9/26 14:45
 * @Version 1.0
 */
@Service
public class SkillLogic {
    @Autowired
    private SkillService skillService;

    public Result add(SkillDTO skillDTO) {
        SkillModel skillModel = new SkillModel();
        BeanUtils.copyProperties(skillDTO, skillModel);
        int cnt = skillService.add(skillModel);
        if (cnt > 0) {
            return Result.generate(0, "Add success", cnt);
        }
        return Result.generate(1, "Add fail", null);
    }

    public Result getById(String noid) {
        if (noid != null || !noid.equals("")) {
            SkillModel skillModel = skillService.getById(noid);
            return Result.generate(0, "Select success", skillModel);
        }
        return Result.generate(1, "Select fail", null);
    }

    public Result upd(String noid, SkillDTO skillDTO) {
        SkillModel skillModel = new SkillModel();
        BeanUtils.copyProperties(skillDTO, skillModel);
        skillModel.setNoid(noid);
        int cnt = skillService.upd(skillModel);
        if (cnt > 0) {
            return Result.generate(0, "Upd success", cnt);
        }
        return Result.generate(1, "Upd fail", null);
    }

    public Result del(String noid) {
        if (noid != null || !noid.equals("")) {
            int cnt = skillService.del(noid);
            if (cnt > 0) {
                return Result.generate(0, "Del success", cnt);
            }
        }
        return Result.generate(1, "Del fail", null);
    }

    public Result selA(SkillDTO skillDTO) {
        SkillModel skillModel = new SkillModel();
        BeanUtils.copyProperties(skillDTO, skillModel);
        List<SkillModel> list = skillService.selA(skillModel);
        if (list.size() > 0) {
            return Result.generate(0, "Select success", list);
        }
        return Result.generate(1, "Select fail", null);
    }
}
